// pages/service/order-info/index.js
var api = require("../../../../api.js"),
  QQMapWX = require("../../../../qqmap-wx.js"),
  app = getApp();
// 播放器
const innerAudioContext = wx.createInnerAudioContext()
Page({

  data: {
    confirmCodeStatus: false, // 确认码显示状态
    shou:'',
    fa:'',
    distype:'',
    soundRecording: {
      tempPath: "",
      duration: "",
      isPlay: false
    }, // 录音
    markers: [],
    latitude: 0,
    longitude: 0,
    // polyline: [{
    //   points: [{
    //     longitude: 0,
    //     latitude: 0
    //   }, {
    //     longitude: 113.524520,
    //     latitude: 23.21329
    //   }],
    //   color: "#FF0000DD",
    //   width: 2,
    //   dottedLine: true
    // }],
    controls: [{
      id: 1,
      iconPath: '/sd_liferuning/resource/common/image/timg.jpg',
      position: {
        left: 0,
        top: 300 - 50,
        width: 50,
        height: 50
      },
      clickable: true,
    }]
  },
  
  markertap(e) {
    let _this = this;
    let markId = e.markerId;
    let marke = _this.data.markers[markId];
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        var latitude = marke.latitude
        var longitude = marke.longitude
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28
        })
      }
    })
  },
  onLoad: function (options) {
  
    /**订单列表 */
    var that = this
    that.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',
    })
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          latitude: latitude,
          longitude: longitude
        })
      }
    })
    app.request({
      url: api.default.stores,
      data: {
        bid: wx.getStorageSync('bid')
      },
      success:function(res){
        console.log(res);
        that.setData({
          distype:res.bid
        })
      }
    })
    app.request({
      url: api.default.ServerOrderInfor,
      data: {
        orderid: options.id
      },
      success: function (res) {
        console.log("sfhasjkfhaskj",res)
        var data = res.data;
        if (data.yinpin != '' && data.yinpin!=0){
          innerAudioContext.src = data.yinpin;
          innerAudioContext.autoplay = true;
          that.data.soundRecording.tempPath = data.yinpin;
          that.data.soundRecording.duration = innerAudioContext.duration;
          
        }
        that.setData({
          data: res.data,
        })
        
        if(data.type=='帮我买'){
          that.setData({
            fa:'购买地址',
            shou:'收货地址'
          })
        } else if(data.type == '帮我送'){
          that.setData({
            fa: '取货地址',
            shou: '收货地址'
          })
        } else if (data.type == '代排队' || data.type == '排队' || data.type == '代办'){
          that.setData({
            fa: '取货地址',
            shou: '服务地址'
          })
        } else if (data.type == '家政') {
          that.setData({
            fa: '服务地址',
          })
        } else if (data.type == '代驾') {
          that.setData({
            fa: '代驾终点',
            shou: '代驾起点'
          })
        } else if (data.type == '代排队' || data.type == '排队') {
          that.setData({
            fa: '服务地址',
          })
        }
        /**订单收发地址 */
        var demo = new QQMapWX({
          key: 'EKJBZ-72L3P-FHXDL-VSLEP-JEAGJ-JTFSD' // 必填
        });
        var address='';
        if (res.data.type == "代排队" || res.data.type == "排队"){
          address= res.data.mudadds
        }else{
          address= res.data.myadds
        }
        demo.geocoder({
          address: address,
          success: function (ress) {
            let markers = that.data.markers;
            markers.push({
              iconPath: "/sd_liferuning/resource/common/image/mudadds.png",
              id: 0,
              latitude: ress.result.location.lat,
              longitude: ress.result.location.lng,
              width: 30,
              height: 30,
              callout: {
                content: that.data.fa,
                color: '#FFFFFF',
                fontSize: 12,
                borderRadius: 3,
                bgColor: '#0FA9F6',
                display: 'ALWAYS',
                textAlign: 'textAlign',
                padding: 10
              }
            });
            that.setData({
              markers: markers
            });
            demo.geocoder({
              address: res.data.mudadds,
              success: function (data) {
                let markers = that.data.markers;
                markers.push({
                  iconPath: "/sd_liferuning/resource/common/image/myadds.png",
                  id: 1,
                  latitude: data.result.location.lat,
                  longitude: data.result.location.lng,
                  width: 30,
                  height: 30,
                  callout: {
                    content: that.data.shou,
                    color: '#FFFFFF',
                    fontSize: 12,
                    borderRadius: 3,
                    bgColor: '#0FA9F6',
                    display: 'ALWAYS',
                    textAlign: 'textAlign',
                    padding: 10
                  }
                });
                that.setData({
                  markers: markers
                });
                console.log('两个标注', that.data.markers)
              },
            });
          }
        })
          },
        });
        
  },
  /**
   * 电话
   */
  toPhone: function (e) {

    var that = this;
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })

  }
  ,
  /**抢单 */
  GiveOrder: function (e) {
  
    app.request({
      url: api.default.GiveOrder,
      data: {
        orderid: e.currentTarget.dataset.id,
        uid: wx.getStorageSync('uid'),
        bid: wx.getStorageSync('bid')
      },
      success: function (res) {
       
        console.log('订单详情', res.data)
        if (res.data == 1) {
          app.request({
            url: api.user.mess,
            data: ({
              bid: wx.getStorageSync('bid'),
              order_no: e.currentTarget.dataset.order_no,
              type:'order'//接单消息类型
            }),
            success: function (rs) {
              console.log(rs)
            }
          })
          wx.showToast({
            title: '恭喜，抢单成功啦！',
            duration:1000,
            success:function(){
              setTimeout(function(){
                wx.redirectTo({
                  url: '/service/pages/service/index/index',
                })
              },1000)
            }
          })
         
        }
      }
    })
  },
  /**完成订单 */
  OkOrder: function (e) {
    let _this =this;
    app.request({
      url: api.default.OkOrder,
      data: {
        orderid: e.currentTarget.dataset.id,
        bid: wx.getStorageSync('bid'),
        uid: wx.getStorageSync('uid'),
        code: _this.data.confirmCode ? _this.data.confirmCode : ''
      },
      success: function (res) {
        console.log('订单详情', res)
        if (res.data == 1) {
          wx.showToast({
            title: '感谢您的付出，您将有丰厚的回报！',
            duration:1000,
            success:function(){
              setTimeout(function(){
                wx.redirectTo({
                  url: '/service/pages/service/index/index',
                })
              },1000)
            }
          })
        }else{
          wx.showToast({
            title: '收货码错误',
            duration: 1000,
          })
        }
      }
    })
  },
  inputConfirmCode:function(e){
    this.setData({
      confirmCode:e.detail.value
    })
  },
  maps:function(e){
    var _this=this;
    var markId = e.currentTarget.dataset.id;
    console.log(markId)
    let marke = _this.data.markers[markId];
    console.log('marke',_this.data.markers)
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        var latitude = marke.latitude
        var longitude = marke.longitude
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28,
        })
      }
    })
  },
  /**
   * 录音播放
   */
  soundRecordingPlay: function () {
    let _this = this;
    let _paused = innerAudioContext.paused;
    let _soundRecording = _this.data.soundRecording;
    if (_paused) {
      innerAudioContext.play();
      _soundRecording.isPlay = true;
      setTimeout(function () {
        let _soundRecording = _this.data.soundRecording;
        _soundRecording.isPlay = false;
        _this.setData({
          soundRecording: _soundRecording
        });
      }, _soundRecording.duration * 1000)
    } else {
      innerAudioContext.stop();
      _soundRecording.isPlay = false;
    }
    _this.setData({
      soundRecording: _soundRecording
    });
  },
  // 预览图片
  previewPic: function (e) {
    let _value = e.currentTarget.dataset.value;
    console.log(e)
    wx.previewImage({
      current: _value, // 当前显示图片的http链接
      urls: [_value] // 需要预览的图片http链接列表
    })
  },
  changeConfirmCodeStatus: function(){
    console.log(this.data.distype)
    if(this.data.distype==1){
      this.setData({
        confirmCodeStatus: !this.data.confirmCodeStatus
      })
    }
  }
})